﻿Imports System.Data
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid
Imports DevExpress.XtraEditors

Public Class HangHoa
    Private state As Integer '0 default 1 tao moi 2 sua
  
    Private Sub setReadonly(ByVal b As Boolean)
        Me.MaTextEdit.Properties.ReadOnly = b
        Me.TenTextEdit.Properties.ReadOnly = b
        Me.LoaiTextEdit.Properties.ReadOnly = b
        Me.VatTextEdit.Properties.ReadOnly = b
        Me.VitriTextEdit.Properties.ReadOnly = b
        Me.MaxTextEdit.Properties.ReadOnly = b
        Me.MinTextEdit.Properties.ReadOnly = b
        Me.NhacungcapTextEdit.Properties.ReadOnly = b
        Me.NhasanxuatTextEdit.Properties.ReadOnly = b
        Me.NuocsanxuatTextEdit.Properties.ReadOnly = b
        If b Then
            Me.IGridView1.OptionsBehavior.Editable = False 'không cho phép chỉnh sửa trên lưới 
            Me.IGridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None 'không cho thêm dòng trống dưới cùng
        Else
            Me.IGridView1.OptionsBehavior.Editable = True 'có cho phép chỉnh sửa trên lưới 
            Me.IGridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom ' cho thêm dòng trống dưới cùng
        End If
    End Sub
    Private Sub choPhepButtonChiTiet(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
    End Sub
    Private Sub choPhepButtonLuuXoa(ByVal b As Boolean)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
        End If
    End Sub
    Private Sub BarButtonItemThoat_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        Try
            Me.setReadonly(False)
            Me.choPhepButtonLuuXoa(True)
            Me.choPhepButtonChiTiet(True)
            Me.MaTextEdit.Focus()
            Me.HangHoaBindingSource.AddNew()
            Me.state = 1
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Frm_DonMau_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PcvgppDataSet.donvitinh' table. You can move, or remove it, as needed.
        Me.DonvitinhTableAdapter.Fill(Me.PcvgppDataSet.donvitinh)
        Me.HanghoaTableAdapter.Fill(Me.PcvgppDataSet.hanghoa)
        Me.Hanghoa_dieukienbaoquanTableAdapter.Fill(Me.PcvgppDataSet.hanghoa_dieukienbaoquan)
        Me.Hanghoa_hoatchatTableAdapter.Fill(Me.PcvgppDataSet.hanghoa_hoatchat)
        Me.Hanghoa_donvitinhTableAdapter.Fill(Me.PcvgppDataSet.hanghoa_donvitinh)
        Me.HanghoaTableAdapter.Fill(Me.PcvgppDataSet.hanghoa)
        Me.KhachhangTableAdapter.Fill(Me.PcvgppDataSet.khachhang)
        setReadonly(True)
        Me.IGridView1.BestFitColumns()
        Me.choPhepButtonChiTiet(False)
        Me.choPhepButtonLuuXoa(False)
        Me.HanghoaBindingSource.MoveLast()
        Me.state = 0
        'Me.IGridView1.OptionsNavigation.AutoFocusNewRow = True
    End Sub

    Private Sub BarButtonItemLuu_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridView1.CloseEditor()
            Me.HangHoaBindingSource.EndEdit()
            Me.FKhanghoadonvitinhhanghoaBindingSource.EndEdit()
            Me.FKhanghoahoatchathanghoaBindingSource.EndEdit()
            Me.HanghoahanghoadieukienbaoquanBindingSource.EndEdit()
            If Me.PcvgppDataSet.HasChanges Then
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Me.HanghoaTableAdapter.Update(Me.PcvgppDataSet.hanghoa)
                    Me.Hanghoa_donvitinhTableAdapter.Update(Me.PcvgppDataSet.hanghoa_donvitinh)
                    Me.Hanghoa_hoatchatTableAdapter.Update(Me.PcvgppDataSet.hanghoa_hoatchat)
                    Me.Hanghoa_dieukienbaoquanTableAdapter.Update(Me.PcvgppDataSet.hanghoa_dieukienbaoquan)
                    Me.state = 0
                    Me.setReadonly(True)
                    Me.choPhepButtonLuuXoa(False)
                    Me.choPhepButtonChiTiet(False)
                End If
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.state = False
                Me.setReadonly(True)
                Me.choPhepButtonLuuXoa(False)
                Me.choPhepButtonChiTiet(False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Function kiemTraMaTonTaiChua() As Boolean 'đảm bảo mã là duy nhất 
        For index = 0 To Me.IGridView1.RowCount - 1
            If Me.IGridView1.GetRowCellValue(index, "ma") = Me.MaTextEdit.EditValue Then
                Return True
                Exit Function
            End If
        Next
        Return False
    End Function

    Private Sub MaTextEdit_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs)
        If kiemTraMaTonTaiChua() And state Then
            Me.MaTextEdit.ErrorText = "Mã đã tồn tại"
            e.Cancel = True
        End If
    End Sub

    Private Sub BarButtonItemSua_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonly(False)
        Me.choPhepButtonLuuXoa(True)
        Me.choPhepButtonChiTiet(True)
        Me.state = 2
    End Sub

    Private Sub BarButtonItemFirst_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.HangHoaBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Me.HangHoaBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonItem6_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick
        Me.HangHoaBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonItem7_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem7.ItemClick
        Me.HangHoaBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Xóa' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.HangHoaBindingSource.RemoveCurrent()
            Me.HangHoaTableAdapter.Update(Me.PcvgppDataSet.hanghoa)
            Me.state = False
            Me.setReadonly(True)
            Me.choPhepButtonLuuXoa(False)
            Me.choPhepButtonChiTiet(False)
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.HangHoaBindingSource.EndEdit()
        If Me.PcvgppDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PcvgppDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub



    Private Sub IGridView1_InvalidRowException(sender As Object, e As DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs) Handles IGridView1.InvalidRowException
        e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction
    End Sub

    Private Sub IGridView1_ShownEditor(sender As Object, e As System.EventArgs) Handles IGridView1.ShownEditor
        Dim view As GridView = CType(sender, GridView)

        ''If TypeOf view.ActiveEditor Is SearchLookUpEdit And IsDBNull(Me.IGridView1.GetRowCellValue(Me.IGridView1.FocusedRowHandle, "ma")) Then
        'If TypeOf view.ActiveEditor Is SearchLookUpEdit Then
        '    DirectCast(view.ActiveEditor, SearchLookUpEdit).ShowPopup()
        'End If
        If view.FocusedColumn.FieldName = "ma" And IsDBNull(Me.IGridView1.FocusedValue) Then
            ' obtaining the active editor 
            Dim Editor As SearchLookUpEdit = view.ActiveEditor
            ' invoking the editor's popup window 
            Editor.ShowPopup()
        End If
    End Sub

    Private Sub IGridView1_ValidatingEditor(sender As Object, e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles IGridView1.ValidatingEditor
        Dim View As GridView = sender
        If View.FocusedColumn.FieldName = "phatsinh" Then
            Dim phatsinh As Double
            Try
                phatsinh = Convert.ToDouble(e.Value)
            Catch ex As Exception

            End Try
            If phatsinh = 0 Then
                e.Valid = False
                e.ErrorText = "Không thể bằng 0"
            End If
        End If
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        IGridView1.Focus()
        IGridView1.AddNewRow()
        batFocusGrid()
    End Sub

    Private Sub BarButtonItem10_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa dòng đang chọn không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) = Windows.Forms.DialogResult.Yes Then
            Dim view As GridView = CType(IGridView1, GridView)
            view.DeleteRow(view.FocusedRowHandle)
        End If
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        Me.FKhanghoadonvitinhhanghoaBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        Me.FKhanghoadonvitinhhanghoaBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        Me.FKhanghoadonvitinhhanghoaBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        Me.FKhanghoadonvitinhhanghoaBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Me.IGridView1.CloseEditor()
        Me.FKhanghoadonvitinhhanghoaBindingSource.EndEdit()
        If Me.PcvgppDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PcvgppDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
    Private Sub IGridView3_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridView3.FocusedRowChanged
        Try
            ' MessageBox.Show(Me.IGridView3.GetRowCellValue(Me.IGridView3.FocusedRowHandle, "ma"))
            ' MessageBox.Show(Me.IGridView3.GetGroupRowValue(Me.IGridView3.FocusedRowHandle))
            Me.HangHoaBindingSource.Position = Me.HangHoaBindingSource.Find("ma", Me.IGridView3.GetGroupRowValue(Me.IGridView3.FocusedRowHandle))
        Catch ex As Exception

        End Try
    End Sub
    Private Sub batFocusGrid()
        '========== bắt focus trên lưới ===========
        IGridView1.FocusedRowHandle = Me.IGridView1.RowCount
        IGridView1.FocusedColumn = IGridView1.VisibleColumns(0)
        IGridView1.ShowEditor()
        '============================
    End Sub
    Private Sub NhasanxuatTextEdit_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles NhasanxuatTextEdit.KeyDown
        If (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) And (state = 1 Or state = 2) Then
            '' 'thêm dòng mới
            'Me.IGridView1.Focus()
            'batFocusGrid()
            'IGridView1.AddNewRow()
            'Me.IGridView1.SetFocusedRowCellValue("ma", Me.MaTextEdit.EditValue)
            IGridView1.Focus()
            IGridView1.AddNewRow()
            batFocusGrid()
        End If
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit5_CloseUp(sender As Object, e As DevExpress.XtraEditors.Controls.CloseUpEventArgs) Handles RepositoryItemSearchLookUpEdit5.CloseUp
        Try
            Dim rowHandle As Integer = RepositoryItemSearchLookUpEdit5.GetIndexByKeyValue(e.Value)
            Dim dr As DataRow = Me.PcvgppDataSet.donvitinh.Rows(rowHandle)
            If Not IsNothing(dr) Then
                If Me.IGridView1.IsNewItemRow(Me.IGridView1.FocusedRowHandle) Then
                    'Me.IGridView1.AddNewRow()
                    Me.IGridView1.SetRowCellValue(IGridView1.FocusedRowHandle, "ma", dr("ma"))
                    Me.IGridView1.SetRowCellValue(IGridView1.FocusedRowHandle, "ten", dr("ten"))
                Else
                    'Me.IGridView1.AddNewRow()
                    'gán cột 'ten' trên searchlookupedit vô cột taikhoanco trong gridview1
                    Me.IGridView1.SetRowCellValue(IGridView1.FocusedRowHandle, "ten", dr("ten"))
                End If

                e.AcceptValue = True
            Else
                e.AcceptValue = False
            End If
        Catch ex As Exception
            e.AcceptValue = False
        End Try
    End Sub

    Private Sub IGridControl3_Click(sender As System.Object, e As System.EventArgs) Handles IGridControl3.Click

    End Sub
    Private Sub ShowGridPreview()
        ' Check whether the XtraGrid control can be previewed. 
        If Not IGridControl1.IsPrintingAvailable Then
            MessageBox.Show("The 'DevExpress.XtraPrinting' Library is not found", "Error")
            Return
        End If
        ' Opens the Preview window. 
        'Me.IGridControl1.ShowPrintPreview()
        'IGridControl1.ShowRibbonPrintPreview()
        Me.IGridControl1.ShowRibbonPrintPreview()
    End Sub
    Private Sub BarButtonItemIn_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemIn.ItemClick
        ShowGridPreview()
    End Sub
End Class